package kr.hkit.info.powerjava.ch23;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class CarGame extends JFrame {
	private JLabel lbl1;
	private JLabel lbl2;
	private JLabel lbl3;
	
	private int x1 =100;
	private int x2 =100;
	private int x3 =100;
	
	class CarThread extends Thread{
		@Override
		public void run() {
			for(int i =0; i<120; i++){
				try {
					sleep(1000);
				} catch (Exception e) {
				}
				
				x1 += Math.random()*10;
				lbl1.setBounds(x1, 0, 100, 100);
				
				x2 += Math.random()*10;
				lbl2.setBounds(x2, 50, 100, 100);
				
				x3 += Math.random()*10;
				lbl3.setBounds(x3, 100, 100, 100);		
			}
		}
	}
	
	public CarGame(){
		setTitle("Car Racing");
		setSize(600,200);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(null);
		
		lbl1 = new JLabel(new ImageIcon("cake.jpg"));
		add(lbl1);
		lbl2 = new JLabel(new ImageIcon("min.jpg"));
		add(lbl2);
		lbl3 = new JLabel(new ImageIcon("sul.jpg"));
		add(lbl3);
		
		lbl1.setBounds(100, 0,	100	,100);
		lbl2.setBounds(100, 50,	100	,100);
		lbl3.setBounds(100, 100,	100	,100);
		
		(new CarThread()).start();
		setVisible(true);		
	}
	
	public static void main(String[] args) {
		new CarGame ();
	}
}
